<?php

namespace app\controller\sys;

use app\BaseController;
use app\model\StationBasic;
use app\model\StationOrder;
use think\Response;
use yly_print\YlyOpen;

class Order extends BaseController
{
    public function lst(): Response
    {
        return $this->success($this->app->make(\app\model\StationOrder::class)->lst());
    }

    public function download(): Response
    {
        return $this->success($this->app->make(\app\model\StationOrder::class)->download());
    }

    public function cancel(): Response
    {
        return $this->success($this->app->make(\app\model\StationOrder::class)->cancel());
    }

    public function review(): Response
    {
        return $this->success($this->app->make(\app\model\StationOrder::class)->review());
    }

    public function refund(): Response
    {
        return $this->success($this->app->make(\app\model\StationOrder::class)->refund());
    }

    public function orderStatic(): Response
    {
        $param = $this->request->param();
        $time = time();
        $last_week = strtotime("-1 week", $time);
        $todayTotal = (new StationOrder)->where('state', 1)->where('payment_time', 'between', [date("Y-m-d 00:00:00"), date("Y-m-d H:i:s")])->when(isset($param['merchant']) && $param['merchant'] != '', function ($query) use ($param) {
            $station = StationBasic::where("mer_id", $param['merchant'])->column("id");
            $query->whereIn('station_id', $station);
        })->sum("money");
        $lastTotal = (new StationOrder)->where('state', 1)->where('payment_time', 'between', [date("Y-m-d 00:00:00", $last_week), date("Y-m-d H:i:s", $last_week)])->when(isset($param['merchant']) && $param['merchant'] != '', function ($query) use ($param) {
            $station = StationBasic::where("mer_id", $param['merchant'])->column("id");
            $query->whereIn('station_id', $station);
        })->sum("money");
        $chatTotal = bcsub($todayTotal, $lastTotal, 2);
        $tip = $chatTotal > 0 ? 'up' : ($chatTotal < 0 ? 'down' : 'over');
        $todayOrder = (new StationOrder)->where('state', 1)->where('payment_time', 'between', [date("Y-m-d 00:00:00"), date("Y-m-d H:i:s")])->when(isset($param['merchant']) && $param['merchant'] != '', function ($query) use ($param) {
            $station = StationBasic::where("mer_id", $param['merchant'])->column("id");
            $query->whereIn('station_id', $station);
        })->field("money,payment_time")->select()->toArray();
        $lastOrder = (new StationOrder)->where('state', 1)->where('payment_time', 'between', [date("Y-m-d 00:00:00", $last_week), date("Y-m-d H:i:s", $last_week)])->when(isset($param['merchant']) && $param['merchant'] != '', function ($query) use ($param) {
            $station = StationBasic::where("mer_id", $param['merchant'])->column("id");
            $query->whereIn('station_id', $station);
        })->field("money,payment_time")->select()->toArray();
        $items = [
            [
                "name" => "今日流水",
                "data" => array_fill(0, (int)date("H") + 1, 0)
            ],
            [
                "name" => "上周流水",
                "data" => array_fill(0, (int)date("H") + 1, 0)
            ],
        ];
        if (!empty($todayOrder)) {
            foreach ($todayOrder as $k => &$v) {
                $h = (int)date("H", strtotime($v['payment_time']));
                $items[0]['data'][$h] += $v['money'];
            }
        }

        if (!empty($lastOrder)) {
            foreach ($lastOrder as $k => &$v) {
                $h = (int)date("H", strtotime($v['payment_time']));
                $items[1]['data'][$h] += $v['money'];
            }
        }

        return $this->success(
            ["data" => $items, "today" => $todayTotal, 'last' => $lastTotal, "tip" => $tip, "chat" => abs($chatTotal), "key" => array_map(function ($v) {
                return ($v < 10 ? "0$v" : $v) . ":00";
            }, array_keys($items[1]['data']))]
        );
    }

    public function orderPrint(): Response
    {
        $order = $this->app->make(StationOrder::class)->with(['station'])->where('id', $this->request->param('id'))->find();
        if (empty($order)) throw new \Exception('订单不存在');
        //打印小票到油站
        $order = $order->toArray();
        $station = $order['station'];
        if (empty($station['print_sn']) || $station['state'] != 1) throw new \Exception('油站未配置打印机');
        $this->app->make(YlyOpen::class, ['id' => $station['print_sn'], 'token' => $station['print_key']])->printStation($station['print_sn'], [
            "station" => $station['name'],
            "order_sn" => $order['order_sn'],
            "date" => $order['payment_time'],
            "gun_name" => $order['gun'],
            "oil_name" => $order['oil'],
            "money" => $order['amount']
        ]);
        return $this->success();
    }
}
